package com.team.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.common.Constants;
import com.team.hiring.common.entity.PageModel;
import com.team.hiring.common.util.FastjsonUtil;
import com.team.hiring.company.dao.PositionDao;
import com.team.hiring.company.entity.CompanyEntity;
import com.team.hiring.company.entity.PositionEntity;
import com.team.hiring.talent.entity.TalentEntity;

/**
 * Servlet implementation class PositionServlet
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PositionDao positionDao = new PositionDao();

	public PositionServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("positionList".equals(command)) {
			positionList(request, response);
		} else if ("goAddPositionUI".equals(command)) {
			goAddPositionUI(request, response);
		} else if ("addposition".equals(command)) {
			addposition(request, response);
		} else if ("updateQuery".equals(command)) {
			updateQuery(request, response);
		} else if ("updatepositionsubmit".equals(command)) {
			updatepositionsubmit(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("selectAllPosition".equals(command)) {
			selectAllPosition(request, response);
		} else if ("homepagePositionDateilInfo".equals(command)) {
			homepagePositionDateilInfo(request, response);
		}else if ("AjaxAddPositon".equals(command)) {
			AjaxAddPositon(request, response);
		}
	}
	
	/**
	 * Ajax追加信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void AjaxAddPositon(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		List<PositionEntity> tt = positionDao.AjaxAddPositon(
				pageNo, pageSize);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(FastjsonUtil.toJson(tt));
		
	}

	/**
	 * 网站主页显示职位的信息
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void selectAllPosition(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		PageModel<PositionEntity> pageModel = positionDao.selectAllPosition(
				pageNo, pageSize);
		request.setAttribute("pageModel", pageModel);
		if (pageModel != null) {
			request.getRequestDispatcher("/WEB-INF/view/common/homepage.jsp")
					.forward(request, response);
		}
		
//		response.setContentType("text/html; charset=UTF-8");
//		response.getWriter().print(FastjsonUtil.toJson(pageModel));
	}

	/**
	 * 列举所有公司职位信息
	 * 
	 * @param request
	 * @param response
	 */
	private void positionList(HttpServletRequest request,
			HttpServletResponse response) {
		CompanyEntity companyEntity = (CompanyEntity) request.getSession()
				.getAttribute("companyEntity");
		int companyid = companyEntity.getId();
		
		List<PositionEntity> positionList = positionDao.positionList(companyid);
		request.setAttribute("positionList", positionList);
		try {
			request.getRequestDispatcher(
					"ApplicationServlet?command=application").forward(request,
					response);
			//WEB-INF/view/company/companymain.jsp
			//<%=request.getContextPath()%>/ApplicationServlet?command=application
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到新增职位界面
	 * 
	 * @param request
	 * @param response
	 */
	private void goAddPositionUI(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher(
					"/WEB-INF/view/company/addposition.jsp").forward(request,
					response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 新增职位
	 * 
	 * @param request
	 * @param response
	 */
	private void addposition(HttpServletRequest request,
			HttpServletResponse response) {
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		CompanyEntity companyEntity = (CompanyEntity) request.getSession()
				.getAttribute("companyEntity");
		int companyId = companyEntity.getId();
		PositionEntity positonEntity = new PositionEntity();
		positonEntity.setCompanyId(companyId);
		positonEntity.setName(name);
		positonEntity.setDuty(duty);
		positonEntity.setAbility(ability);
		positonEntity.setSalary1(salary1);
		positonEntity.setSalary2(salary2);
		boolean result = positionDao.addposition(positonEntity);
		if (result) {
			try {
				request.getRequestDispatcher(
						"PositionServlet?command=positionList").forward(
						request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				// PositionServlet?command=positionList
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			System.err.println("数据库操作成功，返回值这里出错！");
		}

	}

	/**
	 * 修改前的查询，查询完跳转到修改界面
	 * 
	 * @param request
	 * @param response
	 */
	private void updateQuery(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		List<PositionEntity> updateQuery = positionDao.goUpdateUI(id);
		if (updateQuery != null) {
			request.setAttribute("updateQuery", updateQuery);
			try {
				request.getRequestDispatcher(
						"/WEB-INF/view/company/updateposition.jsp").forward(
						request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			System.err.println("updateQuery为空！");
		}
	}

	/**
	 * 提交修改过的职位信息
	 * 
	 * @param request
	 * @param response
	 */
	private void updatepositionsubmit(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		PositionEntity positionEntity = new PositionEntity();
		positionEntity.setName(name);
		positionEntity.setDuty(duty);
		positionEntity.setAbility(ability);
		positionEntity.setSalary1(salary1);
		positionEntity.setSalary2(salary2);
		positionEntity.setId(id);
		int row = positionDao.updatepositionsubmit(positionEntity);
		if (row != 0) {
			try {
				request.getRequestDispatcher(
						"PositionServlet?command=positionList").forward(
						request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("positionId"));
		int result = positionDao.delete(id);
		if (result == 0) {
			// 给页面打印一个
			response.getWriter().print("fail");
		}
	}

	/**
	 * 主页点击职位名称后跳转到详细职位信息界面
	 * 
	 * @param request
	 * @param response
	 */
	private void homepagePositionDateilInfo(HttpServletRequest request,
			HttpServletResponse response) {
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		TalentEntity talentEntity = (TalentEntity)request.getSession().getAttribute("talentEntity");
		if(talentEntity==null){
			PositionEntity homepagePositionDateilInfo = positionDao
					.homepagePositionDateilInfo(positionId);
			request.setAttribute("homepagePositionDateilInfo",
					homepagePositionDateilInfo);
			try {
				request.getRequestDispatcher(
						"/WEB-INF/view/common/positionDetailInfo.jsp").forward(
						request, response);//CommonServlet?command=selectResume
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			PositionEntity homepagePositionDateilInfo = positionDao
					.homepagePositionDateilInfo(positionId);
			request.setAttribute("homepagePositionDateilInfo",
					homepagePositionDateilInfo);
			try {
				request.getRequestDispatcher(
						"ResumeServlet?command=homepageSelectResume&talentId="+talentEntity.getId()).forward(
						request, response);//跳转到ResumeServlet查询简历信息
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
